package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("Event9zxlService")
@Transactional

public class Event9zxlServiceImpl extends CmiEmaService implements Event9zxlService {
	public Event9zxlServiceImpl() {
	}

	@Transactional
	public Event9zxl saveEvent9zxl(Event9zxl event9zxl) {

		if (event9zxl.getId() == null) {

			Event9zxl existingEvent9zxl = event9zxlDAO.findEvent9zxlByPhysicalPrimaryKey(event9zxl.getNumber());

			if (isEquals(event9zxl, existingEvent9zxl)) {
				return existingEvent9zxl;
			}

			event9zxl = event9zxlDAO.store(event9zxl);
			event9zxlDAO.flush();

		} else {
			Event9zxl existingEvent9zxl = event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId());

			event9zxl = event9zxlDAO.store(event9zxl);
			event9zxlDAO.flush();
		}

		return event9zxl;
	}

	public void deleteEvent9zxl(Event9zxl event9zxl) {
		event9zxlDAO.remove(event9zxl);
		event9zxlDAO.flush();
	}

	@Transactional
	public void deleteEvent9zxls(Set<Integer> deleteEvent9zxlsId) {
		Query query = event9zxlDAO.createNamedQuery("deleteEvent9zxlsById", -1, -1);
		query.setParameter(1, deleteEvent9zxlsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Event9zxl> loadEvent9zxls() {
		return event9zxlDAO.findAllEvent9zxls();

	}

	@Transactional
	public Set<Event9zxl> loadReferenceEvent9zxls(Set<Event9zxl> event9zxls) {
		Iterator<Event9zxl> event9zxlsIterator = event9zxls.iterator();
		Set<Event9zxl> referencedEvent9zxls = new HashSet<>();
		while (event9zxlsIterator.hasNext()) {
			Event9zxl currentEvent9zxl = event9zxlsIterator.next();

		}
		return referencedEvent9zxls;
	}

	public Set<Event9zxl> loadEvent9zxlsByIdSet(Set<Integer> event9zxlIdSet) {

		Iterator<Integer> event9zxlIdIterator = event9zxlIdSet.iterator();
		Set<Event9zxl> event9zxlSet = new HashSet<>();
		while (event9zxlIdIterator.hasNext()) {
			Integer id = event9zxlIdIterator.next();
			Event9zxl currentEvent9zxl = event9zxlDAO.findEvent9zxlById(id);
			event9zxlSet.add(currentEvent9zxl);
		}
		return event9zxlSet;
	}

	@Transactional
	public List<Event9zxl> findAllEvent9zxls(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Event9zxl>(event9zxlDAO.findAllEvent9zxls(startResult, maxRows));
	}

	@Transactional
	public Event9zxl findEvent9zxlByPrimaryKey(Integer id) {
		return event9zxlDAO.findEvent9zxlByPrimaryKey(id);
	}

	@Transactional
	public Integer countEvent9zxls() {
		return ((Long) event9zxlDAO.createQuerySingleResult("select count(o) from Event9zxl o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countEvent9zxls(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Event9zxl.class);
		return ((Long) event9zxlDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEvent9zxlsByName(String queryName) {
		Query query = event9zxlDAO.createNamedQuery("countEvent9zxlsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEvent9zxlsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event9zxl.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEvent9zxlObjectReferencedSet(Event9zxl event9zxl) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEvent9zxlClassReferencedSet(Event9zxl event9zxl) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Event9zxl event9zxl, Object obj) {
		if (obj == event9zxl)
			return true;
		if (!(obj instanceof Event9zxl))
			return false;
		Event9zxl comparedEvent9zxl = (Event9zxl) obj;
		if (!CheckPropertyEqual(event9zxl.getNumber(), comparedEvent9zxl.getNumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

